/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_01_Get_Class_Type.java]
 * Creation Date        [20-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_reflection;

/**
 * 反射 - 获得对象或类所属的Class类型
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_01_Get_Class_Type {

	/**
	 * 获得对象或类所属的Super Class类型
	 */
	public static void getSuperClass() {

		System.out.println("Hierarchy of class ......");

		// 注意这里使用class和type的区别
		// Class<?> basicType = Integer.TYPE;
		Class<?> basicType = Integer.class;
		System.out.println("Basic class type is " + basicType);
		Class<?> fatherClass = basicType.getSuperclass();
		System.out.println("Father class type is " + fatherClass);
		Class<?> GrandpaClass = fatherClass.getSuperclass();
		System.out.println("Grandpa class type is " + GrandpaClass);

	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws ClassNotFoundException {
		// 方法1 类名.class
		Class<?> clazz1 = String.class;
		// 方法2 对象名.class
		Class<?> clazz2 = new String().getClass();
		// 方法3 Class.forName(完整的类名)
		Class<?> clazz3 = Class.forName("java.lang.String");
		// class之间的比较用 ==
		System.out.println(clazz1 == clazz2);
		System.out.println(clazz1 == clazz3);

		// 基本类型的类型名 基本类型.class
		System.out.println(int.class.isPrimitive());
		System.out.println(int[].class.isPrimitive());

		// 注意: 封装类的Class对象通过TYPE获得
		System.out.println(int.class == Integer.class);
		System.out.println(int.class == Integer.TYPE);

		Example_01_Get_Class_Type.getSuperClass();
	}

}
